package light.mvc.controller.wxn;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import light.mvc.controller.base.BaseController;
import light.mvc.pageModel.base.Grid;
import light.mvc.pageModel.base.Json;
import light.mvc.pageModel.base.PageFilter;
import light.mvc.pageModel.base.Tree;
import light.mvc.pageModel.wxn.AccessTokenVO;
import light.mvc.pageModel.wxn.WeiMenuVO;
import light.mvc.service.wxn.AccessTokenServiceI;
import light.mvc.service.wxn.WeiMenuServiceI;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/weiMenu")
public class WeiMenuController extends BaseController {
	
	
	
	@Autowired
	private WeiMenuServiceI weiMenuService;

	@Autowired
	private AccessTokenServiceI accessTokenService;
	
	@RequestMapping("/menuCreate")
	@ResponseBody
	public Json menuCreate(String appid) {
		Json j = new Json();
		try {
			if(accessTokenService.menuCrate(appid)){
				j.setSuccess(true);
				j.setMsg("创建成功！");
			}else{
				j.setSuccess(false);
				j.setMsg("创建失败！");
			}
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}
	
	@RequestMapping("/menuDelete")
	@ResponseBody
	public Json menuDelete(String appid) {
		Json j = new Json();
		try {
			if(accessTokenService.menuDelete(appid)){
				j.setSuccess(true);
				j.setMsg("删除成功！");
			}else{
				j.setSuccess(false);
				j.setMsg("删除失败！");
			}
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}
	
	@RequestMapping("/manager")
	public String manager() {
		return "/wxn/weiMenu";
	}

	@RequestMapping("/dataGrid")
	@ResponseBody
	public Grid dataGrid(WeiMenuVO weiMenu, PageFilter ph) {
		Grid grid = new Grid();
		grid.setRows(weiMenuService.dataGrid(weiMenu, ph));
		grid.setTotal(weiMenuService.count(weiMenu, ph));
		return grid;
	}
	
	@RequestMapping("/treeGrid")
	@ResponseBody
	public List<WeiMenuVO> treeGrid() {
		return weiMenuService.treeGrid();
	}
	
	@RequestMapping("/treeMenu")
	@ResponseBody
	public Json treeMenu() {
		Json j = new Json();
		try {
			j.setObj(weiMenuService.treeMenu().toString());
			j.setMsg("成功！");
			j.setSuccess(true);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/tree")
	@ResponseBody
	public List<Tree> tree() {
		return weiMenuService.tree();
	}

	@RequestMapping("/viewPage")
	public String viewPage(HttpServletRequest request, Long id) {
		WeiMenuVO r = weiMenuService.get(id);
		request.setAttribute("weiMenu", r);
		return "/wxn/weiMenuView";
	}

	@RequestMapping("/addPage")
	public String addPage() {
		return "/wxn/weiMenuAdd";
	}

	@RequestMapping("/add")
	@ResponseBody
	public Json add(WeiMenuVO weiMenu) {
		Json j = new Json();
		try {
			weiMenuService.add(weiMenu);
			j.setSuccess(true);
			j.setMsg("添加成功！");
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Json delete(Long id) {
		Json j = new Json();
		try {
			weiMenuService.delete(id);
			j.setMsg("删除成功！");
			j.setSuccess(true);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/get")
	@ResponseBody
	public WeiMenuVO get(Long id) {
		return weiMenuService.get(id);
	}

	@RequestMapping("/editPage")
	public String editPage(HttpServletRequest request, Long id) {
		WeiMenuVO r = weiMenuService.get(id);
		request.setAttribute("weiMenu", r);
		return "/wxn/weiMenuEdit";
	}

	@RequestMapping("/edit")
	@ResponseBody
	public Json edit(WeiMenuVO weiMenu) {
		Json j = new Json();
		try {
			weiMenuService.edit(weiMenu);
			j.setSuccess(true);
			j.setMsg("编辑成功！");
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}
}